<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        default-expand-all
        :expand-on-click-node="false"
        :data="depts"
        :props="defaultProps"
      >
        <!-- 节点结构 -->
        <template v-slot="{ data }">
          <el-row
            style="width: 100%; height: 40px"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <!-- 下拉菜单 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">新增部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 弹层 -->

    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :current-node-id="currentNodeId"
      @addDept="getDepartmentList"
    />
  </div>
</template>
<script>
import { getDepartment, deleteDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  name: 'Department',
  components: { AddDept },
  data() {
    return {
      currentNodeId: null, // 当前节点id
      depts: [], // 数据属性
      defaultProps: {
        children: 'children', // 要显示的字段的名字
        label: 'name' // 读取子节点的字段名
      },
      showDialog: false // 控制弹层显示隐藏
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      // 调取接口数据
      const res = await getDepartment()
      // 接口数据处理（递归）
      this.depts = transListToTreeData(res, 0)
    },
    operateDept(type, id) {
      if (type === 'add') {
        this.currentNodeId = id
        this.showDialog = true
      } else if (type === 'edit') {
        this.currentNodeId = id
        this.showDialog = true
        // 更新props- 异步动作
        // 直接调用了子组件的方法 同步的方法
        // 要在子组件获取数据
        // 父组件调用子组件的方法来获取数据
        this.$nextTick(() => {
          // 等待dom渲染完成
          this.$refs.addDept.operateDept()
        })
      } else if (type === 'del') {
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await deleteDepartment(id)
          this.$message.success('删除成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 25px;
}
</style>
